//*******************************************************************
//  MagazineList.java       Author: Lewis/Loftus
//
//  Represents a collection of magazines.
//*******************************************************************

public class MagazineList
{
   private MagazineNode list;

   //----------------------------------------------------------------
   //  Sets up an initially empty list of magazines.
   //----------------------------------------------------------------
   public MagazineList()
   {
      list = null;
   }

   //----------------------------------------------------------------
   //  Creates a new MagazineNode object and adds it to the end of
   //  the linked list.
   //----------------------------------------------------------------
   public void add(Magazine mag)
   {
      MagazineNode node = new MagazineNode(mag);
      MagazineNode current;

      if (list == null)
         list = node;
      else
      {
         current = list;
         while (current.next != null)
            current = current.next;
         current.next = node;
      }
   }

    public  void insert(int index, String newMagazine) {
       MagazineNode node = new MagazineNode(new Magazine(newMagazine));
       MagazineNode tmp;


       if (list == null)
          list = node;
       else {
          tmp = list;
          for (int n = 0; n < index - 2;n++){
                tmp = tmp.next;
          }
           node.next= tmp.next;
           tmp.next=node;
       }
    }


    public void  delete(String delNode)
    {
        MagazineNode node = list;
        if(node.getMagazine().equals(String.valueOf(delNode)))
        {
            list = node.next;
        }
        else {
            while (!((node.next.getMagazine()).equals(String.valueOf(delNode))))
                node = node.next;
            node.next = (node.next).next;
        }
    }

   //----------------------------------------------------------------
   //  Returns this list of magazines as a string.
   //----------------------------------------------------------------
   public String toString()
   {
      String result = "";

      MagazineNode current = list;

      while (current != null)
      {
         result += current.magazine + "\n";
         current = current.next;
      }

      return result;
   }

   //*****************************************************************
   //  An inner class that represents a node in the magazine list.
   //  The public variables are accessed by the MagazineList class.
   //*****************************************************************
   private class MagazineNode
   {
      public Magazine magazine;
      public MagazineNode next;

      //--------------------------------------------------------------
      //  Sets up the node
      //--------------------------------------------------------------
      public MagazineNode(Magazine mag)
      {
         magazine = mag;
         next = null;
      }
       public String getMagazine()
       {
           return String.valueOf(magazine);
       }
   }


}

